package org.example.sdpback.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.BeanUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.apache.logging.log4j.util.Strings;
import org.example.sdpback.domain.entity.TestCase;
import org.example.sdpback.domain.entity.User;
import org.example.sdpback.domain.enums.TestCaseLevelEnum;
import org.example.sdpback.domain.enums.TestCaseStatusEnum;
import org.example.sdpback.domain.enums.TestCaseTypeEnum;
import org.example.sdpback.domain.vo.IterationVO;
import org.example.sdpback.domain.vo.TestCaseConfigDataVO;
import org.example.sdpback.domain.vo.TestCaseVO;
import org.example.sdpback.mapper.TestCaseMapper;
import org.example.sdpback.service.TestCaseService;
import org.example.sdpback.service.UserService;
import org.example.sdpback.utils.ThreadLocalUtil;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @ProjectName: sdp-back
 * @Package: org.example.sdpback.service.impl
 * @ClassName: TestCaseServiceImpl
 * @Author: zhuxingyue
 * @Description:
 * @Date: 2025/7/24 13:15
 * @Version: 1.0
 **/
@Service
@RequiredArgsConstructor
public class TestCaseServiceImpl extends ServiceImpl<TestCaseMapper, TestCase> implements TestCaseService {

    private final UserService userService;

    @Override
    public void saveTestCase(TestCase testCase) {
        User user = ThreadLocalUtil.get();
        testCase.setCreatorId(user.getId().toString());
        testCase.setCreateTime(LocalDateTime.now());
        testCase.setUpdateTime(LocalDateTime.now());
        this.save(testCase);
    }

    @Override
    public void updateTestCase(TestCase testCase) {
        testCase.setUpdateTime(LocalDateTime.now());
        updateById(testCase);
    }

    @Override
    public TestCase getTestCaseById(Long id) {
        return this.getById(id);
    }

    @Override
    public List<TestCaseVO> getTestCaseList() {
        List<TestCase> list = this.list();
        List<TestCaseVO> testCaseVOS = new ArrayList<>();

        // 需要根据用户ID反馈对应的用户名称
        List<Long> userIds = list.stream().map(TestCase::getCreatorId).collect(Collectors.toList()).stream().map(Long::parseLong).collect(Collectors.toList());
        List<User> users = userService.listByIds(userIds);
        Map<Long, User> userMap = users.stream().collect(Collectors.toMap(User::getId, Function.identity()));

        list.forEach(item -> {
            TestCaseVO testCaseVO = new TestCaseVO();
            testCaseVO.setId(item.getId());
            testCaseVO.setName(item.getName());
            testCaseVO.setLevel(item.getLevel().getName());
            testCaseVO.setType(item.getType().getName());
            testCaseVO.setApiPath(item.getApiPath());
            testCaseVO.setResult(item.getResult());
            testCaseVO.setStatus(item.getStatus().getName());
            testCaseVO.setTag(item.getTag());
            testCaseVO.setCreator(userMap.get(Long.valueOf(item.getCreatorId())).getUsername());
            testCaseVO.setCreateTime(item.getCreateTime().toString());
            testCaseVO.setUpdateTime(item.getUpdateTime().toString());
            testCaseVOS.add(testCaseVO);
        });

        return testCaseVOS;
    }

    @Override
    public List<TestCaseVO> getTestCaseList(String createUser, String  caseName) {
        // 根据创建人，用例名称查询结果
        List<TestCaseVO> testCaseVOS = getTestCaseList();

        if(Strings.isEmpty(createUser)){
            testCaseVOS = testCaseVOS.stream()
                    .filter(item -> Strings.isNotEmpty(caseName) && item.getName().contains(caseName))
                    .collect(Collectors.toList());
            return testCaseVOS;
        }
        if(Strings.isEmpty(caseName)) {
            testCaseVOS = testCaseVOS.stream()
                    .filter(item -> Strings.isNotEmpty(createUser) && item.getCreator().contains(createUser))
                    .collect(Collectors.toList());
            return testCaseVOS;
        }

        testCaseVOS = testCaseVOS.stream()
                .filter(item -> Strings.isNotEmpty(caseName) && item.getName().contains(caseName))
                .filter(item -> Strings.isNotEmpty(createUser) && item.getCreator().contains(createUser))
                .collect(Collectors.toList());

        return testCaseVOS;
    }

    public TestCaseConfigDataVO getConfigData() {
        TestCaseConfigDataVO testCaseConfigDataVO = new TestCaseConfigDataVO();

        testCaseConfigDataVO.setLevelList(Arrays.stream(TestCaseLevelEnum.values())
                .map(item -> {
                    Map<String, Object> map = new HashMap<>();
                    map.put("key", item.getName());
                    map.put("value", item.getValue());
                    return map;
                })
                .collect(Collectors.toList()));

        testCaseConfigDataVO.setTypeList(Arrays.stream(TestCaseTypeEnum.values())
                .map(item -> {
                    Map<String, Object> map = new HashMap<>();
                    map.put("key", item.getName());
                    map.put("value", item.getValue());
                    return map;
                })
                .collect(Collectors.toList()));

        testCaseConfigDataVO.setStatusList(Arrays.stream(TestCaseStatusEnum.values())
                .map(item -> {
                    Map<String, Object> map = new HashMap<>();
                    map.put("key", item.getName());
                    map.put("value", item.getValue());
                    return map;
                })
                .collect(Collectors.toList())); 

        return testCaseConfigDataVO;
    }
}
